#include "../../config.h"

#include <glibmm/i18n.h>

#include "../application.h"
#include "../mainwindow.h"
#include "p_notification.h"
#include "p_macros.h"

namespace gpyp {
namespace pref {

//========================
// Notification
void Notification::test_on_clicked() {
  RefChannel ch = app_.get_selected_channel();
  if (ch) {
    Gtk::Widget *w = format_notebook_.get_current()->get_child();
    if (w == &sw_format_new_) {
      app_.mainwindow()->notify()->notify_new(ch, true);
    } else if (w == &sw_format_change_) {
      app_.mainwindow()->notify()->notify_change(ch, true);
    }
  } else {
    ErrorDialog(parent_, _("No channel selected.")).run();
  }
}

void Notification::providers_on_selected(RefNotifyProviderBase provider) {
  if (provider) {
    timeout_.set_sensitive(provider->has_timeout_support());
    provider->format_new()   ->attach(format_new_   .get_buffer());
    provider->format_change()->attach(format_change_.get_buffer());
    provider_.set_sensitive(true);
  } else {
    provider_.set_sensitive(false);
  }
}

Notification::Notification(Application &app, Gtk::Window &parent) :
  Gtk::VBox    (false, 2),
  app_         (app),
  parent_      (parent),
  notify_error_(_("Show error notification")),
  notify_sound_(app_, parent_),
  provider_    (false, 2)
{
  pack_start(notify_error_, false, false);

  SETUP_SPINBUTTON(timeout_, 0, 100);

  Gtk::Table *t1 = Gtk::manage(new Gtk::Table());
  TABLE_LABEL(*t1, 0, _("Notification timeout"));
  TABLE_LABEL(*t1, 1, _("Notify sound"));
  t1->attach(timeout_,      1, 2, 0, 1);
  t1->attach(notify_sound_, 1, 2, 1, 2);
  t1->set_col_spacings(2);
  t1->set_row_spacings(2);
  pack_start(*t1, false, false);

  sw_format_new_   .set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
  sw_format_new_   .add(format_new_);
  sw_format_change_.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
  sw_format_change_.add(format_change_);
  format_notebook_.append_page(sw_format_new_,    _("New"));
  format_notebook_.append_page(sw_format_change_, _("Change"));

  Gtk::Button *b_t = Gtk::manage(new Gtk::Button(_("Test")));
  b_t->signal_clicked().connect(sigc::mem_fun(*this, &Notification::test_on_clicked));
  Gtk::HButtonBox *h1 = Gtk::manage(new Gtk::HButtonBox(Gtk::BUTTONBOX_END, 2));
  h1->pack_start(*b_t, false, false);

  Gtk::VBox *v1 = Gtk::manage(new Gtk::VBox(false, 2));
  v1->pack_start(format_notebook_);
  v1->pack_start(*h1, false, false);
  v1->set_border_width(2);
  Gtk::Frame *f1 = Gtk::manage(new Gtk::Frame(_("Format")));
  f1->add(*v1);
  f1->set_size_request(-1, 240);
  provider_.set_border_width(2);
  provider_.pack_start(*f1, false, false);

  Gtk::HBox *h2 = Gtk::manage(new Gtk::HBox(false, 8));
  h2->pack_start(*Gtk::manage(new Gtk::Label(_("Backend"))), false, false);
  h2->pack_start(provider_selector_, false, false);
  providers_.set_label_widget(*h2);
  providers_.add(provider_);
  pack_start(providers_, false, false);

  RefNotify notify = app.mainwindow()->notify();
  notify->notify_error()->attach(notify_error_.property_active());
  notify->timeout()     ->attach(timeout_     .property_value());
  notify_sound_.set(notify->notify_sound(), notify->notify_sound_volume());

  const Notify::ProviderList &providers = notify->get_providers();
  if (providers.size()) {
    for(Notify::ProviderList::const_iterator iter = providers.begin(); iter != providers.end(); ++iter) {
      provider_selector_.add(*iter, (*iter)->get_name());
    }
    provider_selector_.signal_selected().connect(sigc::mem_fun(*this, &Notification::providers_on_selected));
    provider_selector_.set_active(notify->get_provider());
  } else {
    providers_.set_sensitive(false);
  }
}

}/*pref*/
}/*gpyp*/
